<?php
/**
 * Created by PhpStorm.
 * User: 王泽锋
 * Date: 2018/03/09
 * Time: 上午 11:23
 */

namespace app\home\controller;

use app\home\model\Shopping_commodity as CommodityModel;

class Commodity extends Base
{
    /**
     * 商城主页
     */
    public function index()
    {
        $CommodityModel = new CommodityModel();
        $map = ['status' => ['eq', 0]];
        $data = $CommodityModel->get_list($map);
        foreach ($data as $v) {
            $v['price'] = number_format($v['price'], 2, ".", "");
            $v['price1'] = floor($v['price']);
            $v['price2'] = mb_substr($v['price'], -2);
        }

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 商城主页商品的详情页
     */
    public function detail()
    {
        $this->anonymous();
        $this->jssdk();
        $id = input('id');
        $CommodityModel = new CommodityModel();
        $map = ['id' => ['eq', $id]];
        $info = $CommodityModel::where($map)->find();
        $info['front_cover'] = get_covers($info['front_cover']);
        $info['price'] = number_format($info['price'], 2, ".", "");
        $info['price1'] = floor($info['price']);
        $info['price2'] = mb_substr($info['price'], -2);

        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 主页列表加载更多
     */
    public function listmore()
    {
        $CommodityModel = new CommodityModel();
        $len = input('length');
        $map = ['status' => ['eq', 0]];
        $list = $CommodityModel->get_lists($map, $len);
        foreach ($list as $v) {
            $v['price'] = number_format($v['price'], 2, ".", "");
            $v['price1'] = floor($v['price']);
            $v['price2'] = mb_substr($v['price'], -2);
        }
        if ($list) {
            return $this->success('加载成功', '', $list);
        } else {
            return $this->error('加载失败');
        }
    }


    /*
    * 搜索功能
    **/
    public function search()
    {
        if (IS_POST) {
            $CommodityModel = new CommodityModel();
            $search = input('val');
            if (!empty($search)) {
                return $CommodityModel->getList($search);
            } else {
                return null;
            }
        } else {
            return $this->fetch();
        }
    }
}